package com.hz.ruoyiequipment.data.controller;

import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentCq;
import com.hz.ruoyiequipment.data.domain.vo.CqEquipment;
import com.hz.ruoyiequipment.data.domain.vo.CqTimeData;
import com.hz.ruoyiequipment.data.service.AgrEquipmentCqService;
import com.hz.ruoyiequipment.data.service.AgrEquipmentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xqd
 * @CreateDate: 2025/03/09/17:06
 * @Description:
 */
@RestController
@RequestMapping("/pest")
@CrossOrigin
public class AgrEquipmentCqController extends BaseController {
    @Autowired
    private AgrEquipmentService agrEquipmentService;
    @Autowired
    private AgrEquipmentCqService agrEquipmentCqService;

    @RequiresPermissions("data:pest:list")
    @GetMapping("/list")
    public TableDataInfo getCqEquipment(CqEquipment cqEquipment) {
        startPage();
        return getDataTable(agrEquipmentService.getCqEquipment(cqEquipment));
    }

    @RequiresPermissions("data:pest:detail")
    @GetMapping("/detail")
    public AjaxResult getCqDetail(Long equipmentId) {
        return agrEquipmentService.getCqDetail(equipmentId);
    }

    @RequiresPermissions("data:pest:insert")
    @PostMapping("/insert")
    public AjaxResult insertCq(@RequestBody CqEquipment cqEquipment) {
        try {
            AgrEquipment agrEquipment = new AgrEquipment();
            agrEquipment.setEquipmentCode(cqEquipment.getEquipmentCode());
            agrEquipment.setEquipmentName(cqEquipment.getEquipmentName());
            agrEquipment.setEquipmentBrand(cqEquipment.getEquipmentBrand());
            agrEquipment.setEquipmentType(cqEquipment.getEquipmentType());
            agrEquipment.setEquipmentImg(cqEquipment.getEquipmentImg());

            AgrEquipmentCq agrEquipmentCq = getAgrEquipmentCq(cqEquipment);
            agrEquipmentService.insertCq(agrEquipment, agrEquipmentCq);
            return AjaxResult.success("新增设备成功");
        } catch (Exception e) {
            return AjaxResult.error("新增设备失败");
        }
    }

    public static AgrEquipmentCq getAgrEquipmentCq(CqEquipment cqEquipment) {
        AgrEquipmentCq agrEquipmentCq = new AgrEquipmentCq();
        agrEquipmentCq.setCqLightWave(cqEquipment.getCqLightWave());
        agrEquipmentCq.setCqCamera(cqEquipment.getCqCamera());
        agrEquipmentCq.setCqSensor(cqEquipment.getCqSensor());
        agrEquipmentCq.setCqCapacity(cqEquipment.getCqCapacity());
        agrEquipmentCq.setCqUpdate(cqEquipment.getCqUpdate());
        return agrEquipmentCq;
    }

    @RequiresPermissions("data:pest:update")
    @PostMapping("/update")
    public AjaxResult updateCq(@RequestBody CqEquipment cqEquipment) {
        return AjaxResult.success(agrEquipmentCqService.updateAgrEquipmentCq(cqEquipment));
    }

    @RequiresPermissions("data:pest:delete")
    @PostMapping("/delete")
    public AjaxResult delCqByEquipmentId(Long equipmentId) {
        return AjaxResult.success(agrEquipmentCqService.delAgrEquipmentCqByEquipmentId(equipmentId));
    }

    @RequiresPermissions("data:pest:timedata")
    @GetMapping("/timedata")
    public AjaxResult getCqTimeData(Long equipmentId) {
        return agrEquipmentCqService.getCqTimeData(equipmentId);
    }


    @RequiresPermissions("data:pest:timedatas")
    @GetMapping("/timedatas")
    public AjaxResult getCqTimeDatas(Long equipmentId) {
        return agrEquipmentCqService.getCqTimeDatas(equipmentId);
    }

    @RequiresPermissions("data:pest:head")
    @GetMapping("/head")
    public AjaxResult getCqTimeDataHead(Long equipmentId) {
        return agrEquipmentCqService.getCqTimeDataHead(equipmentId);
    }

    @RequiresPermissions("data:pest:insertTimeData")
    @PostMapping("/insertTimeData")
    public AjaxResult insertCqTimeData(@RequestBody CqTimeData cqTimeData) {
        return agrEquipmentCqService.insertCqTimeData(cqTimeData);
    }


}
